package com.ruoyi.web.controller.templates;

import com.ruoyi.project.service.ShareService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 首页
 *
 * @author ruoyi
 */
@Controller
public class IndexController
{

    @Resource
    private ShareService shareService;

    @Resource
    private ISysConfigService sysConfigService;

    private static final Map<String,FactorBean> factorDataCache = new ConcurrentHashMap<>();

    private static final double[] iList= new double[]{0.01,0.02,0.03,0.04,0.05,0.06,0.07,0.08,0.09,0.10,
            0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.20};

    private final DecimalFormat df = new DecimalFormat("#.0000");
    /**
     * 访问首页，提示语
     */
    @GetMapping({"/","/index"})
    public String index(Model model, HttpServletRequest request, HttpSession session)
    {
        return "index";
    }


    @GetMapping({"/p/flzz"})
    public String flzz(Model model, HttpServletRequest request, HttpSession session)
    {
        String key = "flzz2030";
        FactorBean factorBean = factorDataCache.get(key);
        if(factorBean==null){
            factorBean = new FactorBean();
            List<List<String>> factorList = new ArrayList<>();
            for(int n=1;n<30;n++){
                List<String> nList = new ArrayList<>();
                for(double i:iList){
                    double iValue = Math.pow((1+i),n);
                    nList.add(df.format(iValue));
                }
                factorList.add(nList);
            }
            factorBean.setFactorList(factorList);
            factorDataCache.put(key,factorBean);
        }
        model.addAttribute("factorList",factorBean.getFactorList());
        return "flzz";
    }

    @GetMapping({"/p/flxz"})
    public String flxz(Model model, HttpServletRequest request, HttpSession session)
    {
        String key = "flxz2030";
        FactorBean factorBean = factorDataCache.get(key);
        if(factorBean==null){
            factorBean = new FactorBean();
            List<List<String>> factorList = new ArrayList<>();
            for(int n=1;n<30;n++){
                List<String> nList = new ArrayList<>();
                for(double i:iList){
                    double iValue = Math.pow((1+i),-n);
                    nList.add(df.format(iValue));
                }
                factorList.add(nList);
            }
            factorBean.setFactorList(factorList);
            factorDataCache.put(key,factorBean);
        }
        model.addAttribute("factorList",factorBean.getFactorList());
        return "flxz";
    }

    @GetMapping({"/p/njxz","/p/njxz/年金现值系数"})
    public String njxz(Model model, HttpServletRequest request, HttpSession session)
    {
        String key = "njxz2030";
        FactorBean factorBean = factorDataCache.get(key);
        if(factorBean==null){
            factorBean = new FactorBean();
            List<List<String>> factorList = new ArrayList<>();
            for(int n=1;n<30;n++){
                List<String> nList = new ArrayList<>();
                for(double i:iList){
                    double iValue = (1-Math.pow((1+i),-n))/i;
                    nList.add(df.format(iValue));
                }
                factorList.add(nList);
            }
            factorBean.setFactorList(factorList);
            factorDataCache.put(key,factorBean);
        }
        model.addAttribute("factorList",factorBean.getFactorList());
        return "njxz";
    }


}
